import pickle, base64
from django_redis import get_redis_connection

def merage_cart_cookie_to_redis(request,response):
    # 获取cookie购物车数据
    # 判断是否有cookie购物车数据
    # 如果没有提前return
    # 将cart_str 转换成 cart_dict

    # 遍历cookie大字典
    # 将sku_id及count向redis的hash中添加
    # 判断当前sku_id是勾选还是未勾选
    # 如果勾选就将当前sku_id添加到set中
    # 如果未勾选就将当前sku_id从set中移除

    # 删除cookie购物车数据
    cart_str = request.COOKIES.get('carts')
    if cart_str is None:
        return
    else:
        cart_dict = pickle.loads(base64.b64decode(cart_str.encode()))

    redis_conn = get_redis_connection('carts')
    user = request.user
    for sku_id in cart_dict:
        redis_conn.hset('cart_%s' % user.id, sku_id, cart_dict[sku_id]['count'])
        if cart_dict[sku_id]['selected']:
            redis_conn.sadd('selected_%s' % user.id, sku_id)
        else:
            redis_conn.srem('selected_%s' % user.id, sku_id)
    response.delete_cookie('carts')

